<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="小区" prop="communityId">
            <communitySelect v-model="queryParams.communityId" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态">
              <el-option
                v-for="(value, key) of statusMap"
                :key="key"
                :label="value"
                :value="key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="姓名" prop="realName">
            <el-input v-model="queryParams.realName" placeholder="请输入姓名" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="borrowList" border>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="业主姓名" align="center" prop="realName" />
      <el-table-column
        label="借用说明"
        align="center"
        min-width="200"
        prop="borrowGoodsDescription"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="申请时间"
        align="center"
        prop="applyTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <span>{{ statusMap[scope.row.status] }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核时间"
        align="center"
        prop="auditTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.auditTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="归还时间"
        align="center"
        prop="returnTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.returnTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="150">
        <template #default="scope">
          <el-button
            v-if="![2, 3].includes(scope.row.status)"
            type="primary"
            link
            icon="Edit"
            @click="handleEdit(scope.row)"
            >修改状态</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <edit
      v-model="dialogVisible"
      v-if="dialogVisible"
      :title="title"
      :row="curRow"
      @submit="getList"
    />
  </div>
</template>

<script setup name="Borrow">
import { getBorrowRecordPage } from "@/api/operateManage";
import { statusMap } from "./config";
import edit from "./edit.vue";
import communitySelect from "@/components/communitySelect/index.vue";
import { ref } from "vue";

const { proxy } = getCurrentInstance();
const borrowList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryParams = ref({
  pageNo: 1,
  pageSize: 10,
  communityId: "",
  status: "",
  realName: "",
});

/** 查询借用记录列表 */
function getList() {
  loading.value = true;
  getBorrowRecordPage(queryParams.value).then(({ data }) => {
    borrowList.value = data.records;
    total.value = data.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

const dialogVisible = ref(false);
const title = ref("");
const curRow = ref({});
/** 新增按钮操作 */
function handleAdd() {
  dialogVisible.value = true;
  title.value = "新增";
  curRow.value = {};
}
function handleEdit(row) {
  dialogVisible.value = true;
  title.value = "修改状态";
  curRow.value = row;
}

/** 状态显示 */
function statusText(status) {
  return statusMap[status] || "未知状态";
}

/** 状态标签类型 */
function statusType(status) {
  const statusMap = {
    0: "",
    1: "warning",
    2: "success",
    3: "danger",
  };
  return statusMap[status] || "info";
}

getList();
</script>
